package work.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import work.entity.Recommend;
import work.service.RecommendService;
import work.vo.DataVo;

import java.util.List;

@RestController
@RequestMapping("/recommend")
public class RecommendHandler {

    @Autowired
    private RecommendService recommendService;

    // 读者荐购
    // 前端传入一个recommend对象
    @PostMapping("/addRecommend")
    public DataVo addRecommend(@RequestBody Recommend recommend) {
        DataVo dataVo = new DataVo();

        if (recommend.getRecommendName().isEmpty() || recommend.getRecommendAuthor().isEmpty()) {
            dataVo.setCode(180);
            dataVo.setMsg("推荐书名及作者不可为空");
            return dataVo;
        }

        recommend.setRecommendId(String.valueOf(System.currentTimeMillis()));

        if (recommendService.insertRecommend(recommend) == 0) {
            dataVo.setCode(181);
            dataVo.setMsg("推荐失败");
        } else {
            dataVo.setCode(182);
            dataVo.setMsg("推荐成功");
        }
        return dataVo;
    }

    // 工作人员查看读者荐购
    // 前端不需要传入任何东西
    @PostMapping("/findRecommend")
    public DataVo findRecommend() {
        DataVo dataVo = new DataVo();

        List<Recommend> recommendList = recommendService.findRecommend();

        if (recommendList.isEmpty()) {
            dataVo.setCode(330);
            dataVo.setMsg("荐购记录为空");
        } else {
            dataVo.setCode(331);
            dataVo.setMsg("存在荐购记录");
            dataVo.setData(recommendList);
        }

        return dataVo;
    }

}